﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Members
	/// </summary>
	public partial class Members
	{
		public Members()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("UserId", "Members"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int UserId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Members");
			strSql.Append(" where UserId=@UserId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4)
			};
			parameters[0].Value = UserId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.Members model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Members(");
			strSql.Append("UserName,Password,GroupId,RealName,Email,Mobile,Phone,Province,City,ProvinceName,CityName,PostCode,Address,Question,Answer,QQ,MSN,IdentityCard,Credits,Pic,CreateTime,UpdateTime,LastLoginTime,IsActive)");
			strSql.Append(" values (");
			strSql.Append("@UserName,@Password,@GroupId,@RealName,@Email,@Mobile,@Phone,@Province,@City,@ProvinceName,@CityName,@PostCode,@Address,@Question,@Answer,@QQ,@MSN,@IdentityCard,@Credits,@Pic,@CreateTime,@UpdateTime,@LastLoginTime,@IsActive)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@GroupId", SqlDbType.Int,4),
					new SqlParameter("@RealName", SqlDbType.NVarChar,10),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.Int,4),
					new SqlParameter("@City", SqlDbType.Int,4),
					new SqlParameter("@ProvinceName", SqlDbType.NVarChar,50),
					new SqlParameter("@CityName", SqlDbType.NVarChar,50),
					new SqlParameter("@PostCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Question", SqlDbType.NVarChar,50),
					new SqlParameter("@Answer", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,50),
					new SqlParameter("@MSN", SqlDbType.NVarChar,50),
					new SqlParameter("@IdentityCard", SqlDbType.NVarChar,50),
					new SqlParameter("@Credits", SqlDbType.Int,4),
					new SqlParameter("@Pic", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@LastLoginTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4)};
			parameters[0].Value = model.UserName;
			parameters[1].Value = model.Password;
			parameters[2].Value = model.GroupId;
			parameters[3].Value = model.RealName;
			parameters[4].Value = model.Email;
			parameters[5].Value = model.Mobile;
			parameters[6].Value = model.Phone;
			parameters[7].Value = model.Province;
			parameters[8].Value = model.City;
			parameters[9].Value = model.ProvinceName;
			parameters[10].Value = model.CityName;
			parameters[11].Value = model.PostCode;
			parameters[12].Value = model.Address;
			parameters[13].Value = model.Question;
			parameters[14].Value = model.Answer;
			parameters[15].Value = model.QQ;
			parameters[16].Value = model.MSN;
			parameters[17].Value = model.IdentityCard;
			parameters[18].Value = model.Credits;
			parameters[19].Value = model.Pic;
			parameters[20].Value = model.CreateTime;
			parameters[21].Value = model.UpdateTime;
			parameters[22].Value = model.LastLoginTime;
			parameters[23].Value = model.IsActive;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Members model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Members set ");
			strSql.Append("UserName=@UserName,");
			strSql.Append("Password=@Password,");
			strSql.Append("GroupId=@GroupId,");
			strSql.Append("RealName=@RealName,");
			strSql.Append("Email=@Email,");
			strSql.Append("Mobile=@Mobile,");
			strSql.Append("Phone=@Phone,");
			strSql.Append("Province=@Province,");
			strSql.Append("City=@City,");
			strSql.Append("ProvinceName=@ProvinceName,");
			strSql.Append("CityName=@CityName,");
			strSql.Append("PostCode=@PostCode,");
			strSql.Append("Address=@Address,");
			strSql.Append("Question=@Question,");
			strSql.Append("Answer=@Answer,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("MSN=@MSN,");
			strSql.Append("IdentityCard=@IdentityCard,");
			strSql.Append("Credits=@Credits,");
			strSql.Append("Pic=@Pic,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("UpdateTime=@UpdateTime,");
			strSql.Append("LastLoginTime=@LastLoginTime,");
			strSql.Append("IsActive=@IsActive");
			strSql.Append(" where UserId=@UserId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@GroupId", SqlDbType.Int,4),
					new SqlParameter("@RealName", SqlDbType.NVarChar,10),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.Int,4),
					new SqlParameter("@City", SqlDbType.Int,4),
					new SqlParameter("@ProvinceName", SqlDbType.NVarChar,50),
					new SqlParameter("@CityName", SqlDbType.NVarChar,50),
					new SqlParameter("@PostCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Question", SqlDbType.NVarChar,50),
					new SqlParameter("@Answer", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,50),
					new SqlParameter("@MSN", SqlDbType.NVarChar,50),
					new SqlParameter("@IdentityCard", SqlDbType.NVarChar,50),
					new SqlParameter("@Credits", SqlDbType.Int,4),
					new SqlParameter("@Pic", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@LastLoginTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4)};
			parameters[0].Value = model.UserName;
			parameters[1].Value = model.Password;
			parameters[2].Value = model.GroupId;
			parameters[3].Value = model.RealName;
			parameters[4].Value = model.Email;
			parameters[5].Value = model.Mobile;
			parameters[6].Value = model.Phone;
			parameters[7].Value = model.Province;
			parameters[8].Value = model.City;
			parameters[9].Value = model.ProvinceName;
			parameters[10].Value = model.CityName;
			parameters[11].Value = model.PostCode;
			parameters[12].Value = model.Address;
			parameters[13].Value = model.Question;
			parameters[14].Value = model.Answer;
			parameters[15].Value = model.QQ;
			parameters[16].Value = model.MSN;
			parameters[17].Value = model.IdentityCard;
			parameters[18].Value = model.Credits;
			parameters[19].Value = model.Pic;
			parameters[20].Value = model.CreateTime;
			parameters[21].Value = model.UpdateTime;
			parameters[22].Value = model.LastLoginTime;
			parameters[23].Value = model.IsActive;
			parameters[24].Value = model.UserId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int UserId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Members ");
			strSql.Append(" where UserId=@UserId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4)
			};
			parameters[0].Value = UserId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string UserIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Members ");
			strSql.Append(" where UserId in ("+UserIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Members GetModel(int UserId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 UserId,UserName,Password,GroupId,RealName,Email,Mobile,Phone,Province,City,ProvinceName,CityName,PostCode,Address,Question,Answer,QQ,MSN,IdentityCard,Credits,Pic,CreateTime,UpdateTime,LastLoginTime,IsActive from Members ");
			strSql.Append(" where UserId=@UserId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4)
			};
			parameters[0].Value = UserId;

			Maticsoft.Model.Members model=new Maticsoft.Model.Members();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UserId"]!=null && ds.Tables[0].Rows[0]["UserId"].ToString()!="")
				{
					model.UserId=int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UserName"]!=null && ds.Tables[0].Rows[0]["UserName"].ToString()!="")
				{
					model.UserName=ds.Tables[0].Rows[0]["UserName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Password"]!=null && ds.Tables[0].Rows[0]["Password"].ToString()!="")
				{
					model.Password=ds.Tables[0].Rows[0]["Password"].ToString();
				}
				if(ds.Tables[0].Rows[0]["GroupId"]!=null && ds.Tables[0].Rows[0]["GroupId"].ToString()!="")
				{
					model.GroupId=int.Parse(ds.Tables[0].Rows[0]["GroupId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RealName"]!=null && ds.Tables[0].Rows[0]["RealName"].ToString()!="")
				{
					model.RealName=ds.Tables[0].Rows[0]["RealName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Mobile"]!=null && ds.Tables[0].Rows[0]["Mobile"].ToString()!="")
				{
					model.Mobile=ds.Tables[0].Rows[0]["Mobile"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Phone"]!=null && ds.Tables[0].Rows[0]["Phone"].ToString()!="")
				{
					model.Phone=ds.Tables[0].Rows[0]["Phone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Province"]!=null && ds.Tables[0].Rows[0]["Province"].ToString()!="")
				{
					model.Province=int.Parse(ds.Tables[0].Rows[0]["Province"].ToString());
				}
				if(ds.Tables[0].Rows[0]["City"]!=null && ds.Tables[0].Rows[0]["City"].ToString()!="")
				{
					model.City=int.Parse(ds.Tables[0].Rows[0]["City"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ProvinceName"]!=null && ds.Tables[0].Rows[0]["ProvinceName"].ToString()!="")
				{
					model.ProvinceName=ds.Tables[0].Rows[0]["ProvinceName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CityName"]!=null && ds.Tables[0].Rows[0]["CityName"].ToString()!="")
				{
					model.CityName=ds.Tables[0].Rows[0]["CityName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PostCode"]!=null && ds.Tables[0].Rows[0]["PostCode"].ToString()!="")
				{
					model.PostCode=ds.Tables[0].Rows[0]["PostCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Address"]!=null && ds.Tables[0].Rows[0]["Address"].ToString()!="")
				{
					model.Address=ds.Tables[0].Rows[0]["Address"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Question"]!=null && ds.Tables[0].Rows[0]["Question"].ToString()!="")
				{
					model.Question=ds.Tables[0].Rows[0]["Question"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Answer"]!=null && ds.Tables[0].Rows[0]["Answer"].ToString()!="")
				{
					model.Answer=ds.Tables[0].Rows[0]["Answer"].ToString();
				}
				if(ds.Tables[0].Rows[0]["QQ"]!=null && ds.Tables[0].Rows[0]["QQ"].ToString()!="")
				{
					model.QQ=ds.Tables[0].Rows[0]["QQ"].ToString();
				}
				if(ds.Tables[0].Rows[0]["MSN"]!=null && ds.Tables[0].Rows[0]["MSN"].ToString()!="")
				{
					model.MSN=ds.Tables[0].Rows[0]["MSN"].ToString();
				}
				if(ds.Tables[0].Rows[0]["IdentityCard"]!=null && ds.Tables[0].Rows[0]["IdentityCard"].ToString()!="")
				{
					model.IdentityCard=ds.Tables[0].Rows[0]["IdentityCard"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Credits"]!=null && ds.Tables[0].Rows[0]["Credits"].ToString()!="")
				{
					model.Credits=int.Parse(ds.Tables[0].Rows[0]["Credits"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Pic"]!=null && ds.Tables[0].Rows[0]["Pic"].ToString()!="")
				{
					model.Pic=ds.Tables[0].Rows[0]["Pic"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdateTime"]!=null && ds.Tables[0].Rows[0]["UpdateTime"].ToString()!="")
				{
					model.UpdateTime=DateTime.Parse(ds.Tables[0].Rows[0]["UpdateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LastLoginTime"]!=null && ds.Tables[0].Rows[0]["LastLoginTime"].ToString()!="")
				{
					model.LastLoginTime=DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsActive"]!=null && ds.Tables[0].Rows[0]["IsActive"].ToString()!="")
				{
					model.IsActive=int.Parse(ds.Tables[0].Rows[0]["IsActive"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserId,UserName,Password,GroupId,RealName,Email,Mobile,Phone,Province,City,ProvinceName,CityName,PostCode,Address,Question,Answer,QQ,MSN,IdentityCard,Credits,Pic,CreateTime,UpdateTime,LastLoginTime,IsActive ");
			strSql.Append(" FROM Members ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" UserId,UserName,Password,GroupId,RealName,Email,Mobile,Phone,Province,City,ProvinceName,CityName,PostCode,Address,Question,Answer,QQ,MSN,IdentityCard,Credits,Pic,CreateTime,UpdateTime,LastLoginTime,IsActive ");
			strSql.Append(" FROM Members ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Members ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.UserId desc");
			}
			strSql.Append(")AS Row, T.*  from Members T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Members";
			parameters[1].Value = "UserId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

